import JQuery from 'jquery';
import { socket } from '@/js/util/connect.js';

let attribute = {
    running: false,
    instanceList: [],
    viewList: [],
    setAttribute: async function(element) {
        this.instanceList = [];
        this.viewList = [];
        if (element.component === 'CloudMap') {
            let url = element.data.url;
            if (url) {
                this.setValue(this.getManagerHostName(), url);
                if (!this.running) {
                    this.running = true;
                    this.instanceList = await this.getInstanceList(url);
                    if (element.data.id) {
                        let instanceList = this.instanceList.filter((item) => item.id === element.data.id);
                        if (instanceList.length) {
                            let viewList = instanceList[0].tours;
                            let list = new Array();
                            if (viewList) {
                                for (let a = 0; a < viewList.length; a++) {
                                    let rs = {};
                                    rs.name = viewList[a];
                                    rs.id = a;
                                    list.push(rs);
                                }
                            }
                            this.viewList = list;
                            if (element.data.viewid >= list.length) {
                                element.data.viewid = -1;
                            }
                        }
                    }
                    this.running = false;
                }
            }
        }
    },
    getInstanceList: async function(url) {
        let data = await socket(url, [
            {
                command: 100,
                details: true
            },
            {
                command: 0
            }
        ]);
        let command = JSON.parse(data[0].data);
        let server = JSON.parse(data[1].data);
        let instances = command.instances;
        let list = new Array();
        if (instances) {
            for (let a = 0; a < instances.length; a++) {
                let instance = {
                    nodeIP: instances[a].nodeIP,
                    id: instances[a].id,
                    playerPort: server.playerPort,
                    projectId: instances[a].projectId,
                    project: instances[a].project,
                    tours: instances[a].tours,
                    viewid: -1
                };
                list.push(instance);
            }
        }
        return list;
    },
    setValue: function(id, value) {
        let obj = JQuery('#' + id);
        obj.val(value);
    },
    getManagerHostName() {
        return window.getRootName() + 'DATA_URL';
    },
    setManagerHost: function(element, value, updateInput = true) {
        element.data.url = value;
        updateInput && this.setValue(this.getManagerHostName(), element.data.url);
    }
};

export default attribute;